<template>
  <div>
    <form ref="loginForm">
      <div class="login">
        <h2 />
        <div class="waveBox">
          <div class="w1" />
          <div class="w2" />
          <div class="w3" />
        </div>
        <div class="loginBox">
          <div class="yhzh_tab_con on saoyisao">
            <p class="title">
              <span>密码登录</span>
            </p>
            <div class="code-box">
              <span class="username">用户名</span>
              <input
                @keyup.enter="handleLogin()"
                v-model="loginForm.username"
                type="text"
                placeholder="请输入用户名"
              />
              <span class="password">密码 </span>
              <input
                @keyup.enter="handleLogin()"
                v-model="loginForm.password"
                type="password"
                placeholder="请输入密码"
              />
              <span class="username">验证码</span>
              <div class="validata">
                <div class="validata_input">
                  <input
                    @keyup.enter="handleLogin()"
                    v-model="loginForm.validCodeValue"
                    type="text"
                    placeholder="请输入验证计算结果"
                  />
                </div>
                <div class="validata_img" @click="validataCode">
                  <img :src="validataCodeImg" alt="" />
                </div>
              </div>
              <input type="button" value="登录" @click="handleLogin" />
            </div>
          </div>
        </div>
        <p class="copyright">
          <span>浙江浙大网新中研软件有限公司</span>
          <span>技术支持热线：0571-88933355</span>
        </p>
      </div>
    </form>
  </div>
</template>

<script>
import '../../assets/loginhtml/css/xiaoshan.css';
import '../../assets/loginhtml/font/iconfont.css';
import { setOauthToken, validataCode } from '@/servers/api';
import { setToken, setUsers } from '@/utils/auth';
export default {
  name: 'Login',
  components: {},
  data() {
    return {
      validataCodeImg: null, // 验证码beas64
      // 登录信息
      loginForm: {
        username: '', // 用户名
        password: '', // 密码
        grant_type: 'password', // 密码登录固定类型
        validCodeId: '', // 验证码code
        validCodeValue: null // 验证码结果
      }
    };
  },
  created() {
    // 获取验证码
    this.validataCode();
  },
  methods: {
    // 登录
    async handleLogin() {
      const { loginForm } = this;
      const res = await setOauthToken({ ...loginForm });
      if (res && res.success) {
        // 使用cooks设置token
        setToken(res.data.accessToken);
        // 设置用户信息
        setUsers('users', res.data.userInfo);
        this.$router.push({
          path: '/dashboard'
        });
      } else {
        this.$message.error(res.errorMsg);
        this.validata();
      }
    },
    /**
     * 验证码
     */
    async validataCode() {
      const res = await validataCode();
      if (res && res.success) {
        const { validateCodeImage, validateCodeKey } = res.data;
        this.validataCodeImg = validateCodeImage;
        this.loginForm.validCodeId = validateCodeKey;
      } else {
        this.$message.error(res.errorMsg);
      }
    }
  }
};
</script>

<style lang="scss">
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.scanCode {
  width: 400px;
  height: 360px;
  margin-top: 30px;
  border: none;
}
.validata {
  display: flex;
  align-items: center;
  .validata_input {
    flex: 1;
    margin-right: 10px;
  }
  .validata_img {
    width: 124px;
    height: 40px;
    cursor: pointer;
    margin-top: 10px;
    img {
      width: 100%;
      height: 100%;
    }
  }
}
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
